package com.binvshe.binvshe.mine.ticket;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.binvshe.binvshe.account.ParamKey;
import com.binvshe.binvshe.account.utils.FragmentUtils;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.UrlParams;

/**
 * FileName: LoseTicketFragment
 *
 * @CretedBy : zofnk
 * @CreateDate: 2016/9/21 17:26
 * @E-mail: zofnk@vip.qq.com
 * @RevisionsDate：
 * @ModifyNotes：
 */
public class LoseTicketFragment extends SwipeListFragment {

    private int mPage = 0;

    public static LoseTicketFragment newInstance() {

        Bundle args = new Bundle();
        LoseTicketFragment fragment = new LoseTicketFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        showToolbar();
        getCustomToolbar().setTitle("已失效票券");
        getCustomToolbar().enableBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentUtils.backStack(getActivity());
            }
        });
        return view;
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        TicketAdapter ticketAdapter = new TicketAdapter(getContext());
        ticketAdapter.setIsLoseTicket(false);
        ticketAdapter.setFragmentManager(getFragmentManager());
        return ticketAdapter;
    }

    @Override
    public void onResume() {
        super.onResume();
        startLoading();
    }

    @Override
    public void onStartRefreshing() {
        mPage = 0;
        getData().clear();
        onStartLoading();
        super.onStartRefreshing();
    }

    @Override
    public void onStartLoading() {
        getTicketList();
    }

    private ListDataRequestListener<TicketEntity> mResponceListener = new ListDataRequestListener<>(this, TicketEntity.class, true);

    public void getTicketList() {
        if (mPage == 0 || mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
            MyHttpRequest request = new MyHttpRequest(getContext());
            UrlParams urlParams = new UrlParams();
            urlParams.put(ParamKey.PAGE, mPage + "");
            urlParams.put(ParamKey.SIZE, 8 + "");
            request.get(UrlCenter.VOUCHERS_INVALID, urlParams, mResponceListener);
        }
    }
}
